package com.scalesoft.artsync.frontend.model;

import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;

import com.scalesoft.artsync.model.submission.Submission;

public class SubmissionTableModel extends AbstractTableModel {

	private static final long serialVersionUID = -3196022652998210701L;
	private String[] columnNames = { "Select", "Icon", "Title", "Tags", "Description" };
	private ArrayList<Submission> data = new ArrayList<Submission>();

	public int getColumnCount() {
		return columnNames.length;
	}

	public int getRowCount() {
		return data.size();
	}

	public String getColumnName(int col) {
		return columnNames[col];
	}

	public Object getValueAt(int row, int col) {
		switch (col) {
		case 0:
			return data.get(row).isSelected();
		case 1:
			return data.get(row).getIcon();
		case 2:
			return data.get(row).getTitle();
		case 3:
			return data.get(row).getTags();
		case 4:
			return data.get(row).getDescription();
		}
		return null;
	}

	public Class<?> getColumnClass(int c) {
		Object value = getValueAt(0, c);
		if (value != null)
			return value.getClass();

		return String.class;
	}

	public boolean isCellEditable(int row, int col) {
		if (col <= 0) {
			return true;
		}
		return false;
	}

	public ArrayList<Submission> getData() {
		return data;
	}

	/*
	 * Don't need to implement this method unless your table's data can change.
	 */
	public void setValueAt(Object value, int row, int col) {
		System.out.println("Setting value at " + row + "," + col + " to " + value + " (an instance of "
				+ value.getClass() + ")");

		if (value instanceof Boolean) {
			data.get(row).setSelected((Boolean) value);
		}
		fireTableCellUpdated(row, col);
	}

	public void setData(ArrayList<Submission> data) {
		this.data = data;
		this.fireTableDataChanged();
	}

}
